package com.macro.mall.demo.pattern.chain;

import lombok.Data;

import java.util.Objects;

@Data
public abstract class AbstractBusinessChain<T> implements BusinessChain<T> {

    protected BusinessChain<T> next;

    private Boolean hasNext() {
        return Objects.nonNull(next);
    }

    @Override
    public void process(T param) {
        Boolean r = executeProcess(param);
        // 自己处理不了自动流转到下一个
        if (Boolean.FALSE.equals(r) && Boolean.TRUE.equals(hasNext())) {
            next.process(param);
        }
    }

    protected abstract Boolean executeProcess(T param);

    @Override
    public void setNext(BusinessChain<T> next) {
        this.next = next;
    }
}